package com.px.user.mapper;

import com.px.user.entity.ChatRobot;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ChatRobotMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    @Insert("insert into chat_robot (creatorId,category,contain,response,content) " +
            "values (#{creatorId},#{category},#{contain},#{response},#{content})")
    void insert(ChatRobot chatRobot);

    @Update("update chat_robot set category=ifnull(#{category},category),contain=ifnull(#{contain},contain)," +
            "response=ifnull(#{response},response),content=ifnull(#{content},content) " +
            "where id=#{id}")
    void update(ChatRobot chatRobot);

    @Delete("delete from chat_robot where id=#{id}")
    void delete(Integer id);

    @Select({"<script>",
            "select * from chat_robot",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='qstart != null'>and createTime >= #{qstart}</if>",
            " <if test='qend != null'>and createTime <![CDATA[<=]]> #{qend}</if>",
            " <if test='categoryList != null'>and category in ",
            "  <foreach item='item' index='index' collection='categoryList' open='(' separator=',' close=')'>#{item}",
            " </foreach></if>",
            " <if test='contain != null'>and contain like #{contain}</if>",
            " <if test='response != null'>and response like #{response}</if>",
            " <if test='content != null'>and content like #{content}</if>",
            "</where>",
            "order by ${orderField} desc limit #{startIndex},#{limit}",
            "</script>"})
    List<ChatRobot> paging(ChatRobot chatRobot);

    @Select({"<script>",
            "select count(1) from chat_robot",
            "<where>",
            " <if test='id != null'>and id = #{id}</if>",
            " <if test='qstart != null'>and createTime >= #{qstart}</if>",
            " <if test='qend != null'>and createTime <![CDATA[<=]]> #{qend}</if>",
            " <if test='categoryList != null'>and category in ",
            "  <foreach item='item' index='index' collection='categoryList' open='(' separator=',' close=')'>#{item}",
            " </foreach></if>",
            " <if test='contain != null'>and contain like #{contain}</if>",
            " <if test='response != null'>and response like #{response}</if>",
            " <if test='content != null'>and content like #{content}</if>",
            "</where>",
            "</script>"})
    Integer pagingCount(ChatRobot chatRobot);
}
